<?php
class Db{
    private $host;
    private $root;
    private $password;
    private $datebase;
    private $port;
    public $conn;//保存数据库链接
    public function __construct($root,$password,$datebase,$host='localhost',$port='8080')
    {
        $this->host=$host;
        $this->root=$root;
        $this->password=$password;
        $this->datebase=$datebase;
        $this->port=$port;
        $this->connectDb();
    }

    public function connectDb(){
        $this->conn=mysqli_connect($this->host,$this->root,$this->password,$this->datebase);
        //检测数据库是否链接成功
if($this->conn){
    echo '数据库链接成功';
}else{
    echo '数据库链接失败'.mysqli_error($this->conn);
}
    }

//增加数据
public function write(){
$sql="INSERT INTO user(`name`,`num`) VALUES('账上','125')";
if(mysqli_query($this->conn,$sql)){
    echo '插入成功';
}else{
    echo '插入失败'.mysqli_error($this->conn);
}

}
//修改
public function edi($sql){
    
    if(mysqli_query($this->conn,$sql)){
        echo '修改成功';
    }else{
        echo '修改失败'.mysqli_error($this->conn);
    }
}
//查询
public function del(){
    $sql="DELETE FROM user WHERE  id=3";
    if(mysqli_query($this->conn,$sql)){
        echo '删除成功';
    }else{
        echo '删除失败'.mysqli_error($this->conn);
    }
}

// public function research(){
// $sql="SELECT * FROM user";

// if(mysqli_query($this->conn,$sql)){
//     echo '查询成功';
// }else{
//     echo '查询失败'.mysqli_error($this->conn);
// }
// }


public function research($sql){
   $result=mysqli_query($this->conn,$sql);
   if(mysqli_affected_rows($this->conn)>0){
       return mysqli_fetch_all($result);

   }else{
       return "失败".mysqli_error($this->conn);

   }
}

}

$db=new Db('root','root','text','127.0.0.1');
$sql="UPDATE user SET name='frhgfd' where id=2";
// $db->write();
$db->edi($sql);
$db->del();
?>